#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    ui->pushButton->setEnabled(false);
    ui->lineEdit_original->setFocus();
}

Dialog::~Dialog()
{
    delete ui;
}

//设置最终地址
void Dialog::SetURL(QString url)
{
    QString url_thunder,url_flashget,url_qqdl;
    QByteArray real,thunder,flashget,qqdl;

    //添加迅雷地址特征信息
    url_thunder = "AA" + url + "ZZ";
    //添加快车地址特征信息
    url_flashget = "[FLASHGET]" + url + "[FLASHGET]";

    //用toLocal8Bit()转换，防止中文乱码
    real.append(url.toLocal8Bit());
    thunder.append(url_thunder.toLocal8Bit());
    flashget.append(url_flashget.toLocal8Bit());
    qqdl.append(url.toLocal8Bit());

    ui->lineEdit_real->setText(QString::fromLocal8Bit(real.data()));
    //添各类型加地址前缀并输出
    ui->lineEdit_thunder->setText("thunder://" + thunder.toBase64());
    ui->lineEdit_flashget->setText("flashget://" +flashget.toBase64());
    ui->lineEdit_qqdl->setText("qqdl://" + qqdl.toBase64());
}

void Dialog::on_pushButton_clicked()
{
    /*
     *为了区分输入地址的不同类型，需要取其地址的前几位进行判断
     *temp_4chars   www.
     *temp_6chars   ftp://
     *temp_7chars   http:// ed2k:// qqdl://
     *temp_8chars   https://
     *temp_10chars  thunder://
     *temp_11chars  flashget://
     */
    QString url,tempurl,trueurl,temp_4chars,temp_6chars,temp_7chars,temp_8chars,temp_10chars,temp_11chars;
    QByteArray tempba;
    //int key = 0;
    url.append(ui->lineEdit_original->text()).toLocal8Bit();
    temp_4chars = url.left(4);
    temp_6chars = url.left(6);
    temp_7chars = url.left(7);
    temp_8chars = url.left(8);
    temp_10chars = url.left(10);
    temp_11chars = url.left(11);

    //安装下载工具后，监视剪贴板可以直接下载的地址类型
    if (temp_6chars == QString("ftp://") || temp_7chars == QString("http://") ||
        temp_7chars == QString("ed2k://") || temp_8chars == QString("https://") ||
        temp_4chars == QString("www."))
    {
        trueurl = url;
        SetURL(trueurl);
    }
    //迅雷专用地址
    else if (temp_10chars == QString("thunder://"))
    {
        tempba.append(url.right(url.length() - 10));
        tempurl = QString::fromLocal8Bit(QByteArray::fromBase64(tempba).data());
        trueurl = tempurl.mid(2,tempurl.length() - 4);
        SetURL(trueurl);
    }
    //快车专用地址
    else if (temp_11chars == QString("flashget://"))
    {
        tempba.append(url.right(url.length() - 11));
        tempurl = QString::fromLocal8Bit(QByteArray::fromBase64(tempba).data());
        trueurl = tempurl.mid(10,tempurl.length() - 20);
        SetURL(trueurl);
    }
    //旋风专用地址
    else if (temp_7chars == QString("qqdl://"))
    {
        tempba.append(url.right(url.length() - 7));
        tempurl = QString::fromLocal8Bit(QByteArray::fromBase64(tempba).data());
        trueurl = tempurl;
        SetURL(trueurl);
    }
    //其他类型的地址
    else
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("友情提示"),QString::fromLocal8Bit("暂不支持此类型的地址转换"),QMessageBox::Ok);

        ui->lineEdit_real->setText(QString::null);
        ui->lineEdit_thunder->setText(QString("thunder://"));
        ui->lineEdit_flashget->setText(QString("flashget://"));
        ui->lineEdit_qqdl->setText(QString("qqdl://"));
    }

}

//当原始地址栏有输入时，才将转换按钮置为可用
void Dialog::on_lineEdit_original_textChanged(QString )
{
    ui->pushButton->setEnabled(true);
}
